package com.example.photo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.photo.common.ErrorCode;
import com.example.photo.constant.ContestStatus;
import com.example.photo.exception.BusinessException;
import com.example.photo.mapper.ContestMapper;
import com.example.photo.model.VO.ContestVO;
import com.example.photo.model.domain.Contest;
import com.example.photo.model.request.add.NewContestRequest;
import com.example.photo.model.request.update.UpdateContestRequest;
import com.example.photo.service.ContestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author 85792
 * @description 针对表【contest(赛事)】的数据库操作Service实现
 * @createDate 2023-03-25 19:14:42
 */
@Service
public class ContestServiceImpl extends ServiceImpl<ContestMapper, Contest>
        implements ContestService {

    @Resource
    private ContestMapper contestMapper;


    /**
     * 新建赛事
     */
    @Override
    public ContestVO newContest(NewContestRequest newContestRequest) {
        Contest contest = new Contest();
        contest.setBannerPic(newContestRequest.getBannerPic());
        contest.setTitle(newContestRequest.getTitle());
        contest.setSubtitle(newContestRequest.getSubtitle());
        contest.setStartTime(LocalDateTime.parse(newContestRequest.getStartTime()));
        contest.setStopTime(LocalDateTime.parse(newContestRequest.getStopTime()));
        contest.setIntroductionPic(newContestRequest.getIntroductionPic());
        contest.setStatus(newContestRequest.getStatus() == null ? ContestStatus.NOT_STARTED : newContestRequest.getStatus());
        contest.setTailPic(newContestRequest.getTailPic());
        contest.setQuestionnaire(newContestRequest.getQuestionnaire());
        contestMapper.insert(contest);
        return new ContestVO(contest);
    }

    /**
     * 更新赛事信息
     */
    @Override
    public void updateContest(UpdateContestRequest updateContestRequest) {
        if (updateContestRequest.getContestId() == null || updateContestRequest.getContestId() < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Contest contest = new Contest(updateContestRequest);
        if (contestMapper.updateById(contest) < 1) {
            throw new BusinessException(ErrorCode.SERVER_ERROR, "更新赛事信息失败");
        }
    }
}




